package com.jhws.business.customer.controller;

import java.util.List;
import java.util.Map;

import com.jhws.business.customer.bo.CustConstants;
import com.jhws.business.customer.bo.SdkCustomers;
import com.jhws.core.push.PushXinGe;
import com.jhws.sys.menu.bo.TblMenuDimBo;
import com.jhws.sys.user.bo.UserConstant;
import com.jhws.sys.user.bo.UserPerms;
import com.jhws.sys.user.service.ITblUserDimService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.util.GetId;
import com.jhws.business.customer.service.SdkCustService;

import javax.annotation.Resource;


@Controller
@RequestMapping("/customer")
public class SdkCustCtrl {

	@Resource
	private SdkCustService sdkCustService;

	@Resource
	private RedisTemplate stringTemplate;

	@Resource
	private ITblUserDimService userDimService;

	@RequestMapping("addCustom.x")
	@RequiresRoles("admin")
	ModelAndView addCustom(SdkCustomers customers) throws Exception {
		ModelAndView mv = new ModelAndView();
		String ID = GetId.GetId2();
		if (customers == null) {
			customers = new SdkCustomers();
			ID = ID.substring(ID.length() - 10, ID.length());
			customers.setCustomId(ID);
		} else if (customers.getId() == null) {
			ID = ID.substring(ID.length() - 10, ID.length());
			customers.setCustomId(ID);
		}else{
			mv.addObject("modify","true");
			/* 将原有的包名记录下来,传给前端,为了保存时删除 */
			mv.addObject("oldAndroidPkgName",customers.getAndroidPackageName());
			mv.addObject("oldIOSPkgName",customers.getIOSPackageName());
		}
		mv.addObject("Customers", customers);
		mv.setViewName("business/customer/addCustomer");
		return mv;
	}

	@RequestMapping("save.x")
	@ResponseBody
	public JsonRespData saveCustom(SdkCustomers customers, String modify, String oldAndroidPkgName, String oldIOSPkgName) {
		if(StringUtils.isEmpty(modify) && sdkCustService.isCustomNameExist(customers.getCustomName())){
			return new JsonRespData("", -1, "添加失败，客户ID名已经存在！");
		}
		/* 假如信鸽相关信息变了, 一定要清空缓存 */
		if(StringUtils.isNotEmpty(modify)){
			stringTemplate.delete(PushXinGe.REDIS_XINGE_APPID);
		}

		sdkCustService.saveCustomer(customers);
		HashOperations hashOperations = stringTemplate.opsForHash();

		if(StringUtils.isNotEmpty(modify)){
			hashOperations.delete(CustConstants.REDIS_CUSTOMER_HASH,customers.getCustomId()+"-"+oldAndroidPkgName);
			hashOperations.delete(CustConstants.REDIS_CUSTOMER_HASH,customers.getCustomId()+"-"+oldIOSPkgName);
		}

		if(StringUtils.isNotEmpty(customers.getAndroidPackageName()))
			hashOperations.put(CustConstants.REDIS_CUSTOMER_HASH,customers.getCustomId()+"-"+customers.getAndroidPackageName(),""+customers.getIsEnable());
		if(StringUtils.isNotEmpty(customers.getIOSPackageName()))
			hashOperations.put(CustConstants.REDIS_CUSTOMER_HASH,customers.getCustomId()+"-"+customers.getIOSPackageName(),""+customers.getIsEnable());
		return new JsonRespData("", 1, "");
	}


	@RequestMapping(value = "deleteCustomer.x",method = RequestMethod.POST)
	@ResponseBody
	public JsonRespData deleteCustom(String id) {
		SdkCustomers customer = sdkCustService.findCustomById(id);
		sdkCustService.deleteCustom(id);
		HashOperations hashOperations = stringTemplate.opsForHash();
		String androidKey = customer.getCustomId()+"-"+customer.getAndroidPackageName();
		String IOSKey = customer.getCustomId()+"-"+customer.getIOSPackageName();
		hashOperations.delete(CustConstants.REDIS_CUSTOMER_HASH,androidKey,IOSKey);
		return new JsonRespData("", 1, "");
	}

	@RequestMapping("querySdkCustPage.x")
	@ResponseBody
	JsonRespData querySdkCustPage(@RequestParam Map param) {
		Map<String, Object> resources = sdkCustService.queryCustomerList(param);
		return new JsonRespData("", 1, resources);
	}

    /**
     * 获取已分配的权限
     * @param id
     * @return
     */
    @RequestMapping("getPermsOfCustomId.x")
	@ResponseBody
	public JsonRespData getPermsOfCustomId(String id){
		List<TblMenuDimBo> perms =  userDimService.findUserPermsObjects(id);
		return new JsonRespData("", 1, perms);
	}

    /**
     * 保存权限
     * @return
     */
    @RequestMapping("saveCustomerPerms.x")
    @ResponseBody
    public JsonRespData saveCustomerPerms(String customId, String [] menuCodes){
        UserPerms userPerms;
        for (String menuCode : menuCodes){
            userPerms = new UserPerms();
            userPerms.setMenuCode(menuCode);
            userPerms.setUserCode(customId);
            userPerms.setType(UserConstant.USER_PERMS_SDK_TYPE);
            userDimService.saveUserPerms(userPerms);
        }
        return new JsonRespData("", 1, "");
    }
}
